<template>
  <div class="forgot-password-container">
    <el-card class="forgot-password-card">
      <div class="header">
        <h2>找回密码</h2>
        <p>请输入您的账号信息</p>
      </div>
      
      <el-form :model="passwordForm" :rules="rules" ref="passwordFormRef">
        <el-form-item prop="role">
          <el-select v-model="passwordForm.role" placeholder="请选择用户类型" class="form-input">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="医生" value="doctor"></el-option>
            <el-option label="患者" value="patient"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item prop="id">
          <el-input v-model="passwordForm.id" placeholder="请输入账号" class="form-input"></el-input>
        </el-form-item>
        
        <el-form-item prop="email">
          <el-input v-model="passwordForm.email" placeholder="请输入注册时的电子邮箱" class="form-input"></el-input>
        </el-form-item>
        
        <el-form-item prop="newPassword">
          <el-input v-model="passwordForm.newPassword" type="password" placeholder="请输入新密码" class="form-input"></el-input>
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input v-model="passwordForm.confirmPassword" type="password" placeholder="请确认新密码" class="form-input"></el-input>
        </el-form-item>
        
        <div class="form-actions">
          <el-button type="primary" :loading="loading" @click="handleResetPassword" class="reset-btn">重置密码</el-button>
        </div>
        
        <div class="form-links">
          <router-link to="/login">返回登录</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useAuthStore } from '../../stores/auth'
import { ElMessage } from 'element-plus'

const authStore = useAuthStore()
const loading = ref(false)
const passwordFormRef = ref(null)

const passwordForm = reactive({
  role: 'patient',
  id: '',
  email: '',
  newPassword: '',
  confirmPassword: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else {
    if (passwordForm.confirmPassword !== '') {
      passwordFormRef.value.validateField('confirmPassword')
    }
    callback()
  }
}

const validateConfirmPass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认新密码'))
  } else if (value !== passwordForm.newPassword) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  role: [{ required: true, message: '请选择用户类型', trigger: 'change' }],
  id: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入电子邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  newPassword: [{ validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ validator: validateConfirmPass, trigger: 'blur' }]
}

const handleResetPassword = async () => {
  if (!passwordFormRef.value) return
  
  try {
    await passwordFormRef.value.validate()
    loading.value = true
    
    await authStore.recoverPassword(passwordForm)
    
    if (authStore.error) {
      ElMessage.error(authStore.error)
    } else {
      ElMessage.success('密码重置成功，请使用新密码登录')
    }
  } catch (validationError) {
    console.error('Validation error:', validationError)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.forgot-password-card {
  width: 400px;
  padding: 20px;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 8px;
}

.header p {
  color: #909399;
  font-size: 14px;
}

.form-input {
  width: 100%;
}

.form-actions {
  margin-top: 24px;
}

.reset-btn {
  width: 100%;
}

.form-links {
  margin-top: 16px;
  display: flex;
  justify-content: center;
  font-size: 14px;
}

.form-links a {
  color: #409eff;
  text-decoration: none;
}
</style> 